<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container" v-if="isBind">
    <div class="unbind">
      <div class="loading"></div>
    </div>
  </section>
  <section class="container" v-else>
    <nav class="tab">
      <a @click="hasAccount=true" :class="{active:hasAccount}" href="javascript:;">
        <i class="iconfont icon-bind" />
 <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a @click="hasAccount=false" :class="{active:!hasAccount}" href="javascript:;">
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <CallbackBind :unionId='unionId'/>
    </div>
    <div class="tab-content" v-else>
      <CallbackPatch :unionId='unionId'/>
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import Message from '@/components/library/Message'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { userQQLogin } from '@/api/user'
import { ref } from 'vue'
import LoginHeader from './components/login-header'
import LoginFooter from './components/login-footer'
import CallbackBind from './components/callback-bind'
import CallbackPatch from './components/callback-patch'
export default {
  name: 'PageCallback',
  components: { LoginHeader, LoginFooter, CallbackBind, CallbackPatch },
  setup () {
    // tab切换
    const hasAccount = ref(true)
    // 检查QQ是否已经登录
    // console.dir(QC.Login)
    // const res = QC.Login.check()
    // console.log(11111111111, '检查是否已经登录', res)
    // 假设已经绑定，默认会去做一次登录，如果登录失败证明未绑定。
    const isBind = ref(true)
    const store = useStore()
    const router = useRouter()
    const unionId = ref(null)
    // 获取当前登录QQ用户的Access Token以及OpenID(身份证)
    if (QC.Login.check()) {
      QC.Login.getMe(async (openId) => {
      // console.log({ openId, accessToken })
        unionId.value = openId
        try {
          const { result } = await userQQLogin(openId)
          store.commit('user/setUser', result)
          if (store.state.user.redirectUrl) {
            router.push(store.state.user.redirectUrl)
          } else {
            router.back()
          }
          Message({ type: 'success', text: 'QQ登录成功' })
          // 登录成功后合并购物车数据
          store.dispatch('cart/mergeLocalCart')
        } catch (error) {
          isBind.value = false
        }
      })
    }

    return { hasAccount, unionId, isBind }
  }
}
</script>

<style scoped lang='less'>
.container {
  padding: 25px 0;
  position: relative;
  height: 730px;
  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;
    .loading {
      height: 100%;
      background: #fff url(~@/assets/images/load.gif) no-repeat center / 100px 100px;
    }
  }
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
